package com.safety.hat.config.pool;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author: lijun
 * @date: 2020/5/25 下午3:49
 */
@Configuration
@EnableScheduling
@EnableAsync
public class ThreadPoolTaskExecutorAutoConfig {

    /**
     * 基础任务池
     *
     * @return
     */
    @Bean(name = TaskBeanNameCommon.COMMON)
    @Primary
    public ThreadPoolTaskExecutor commonTaskPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(2000);
        executor.setThreadFactory((runnable) -> {
            Thread thread = new Thread(runnable);
            String threadName = "common" + "-" + thread.getName();
            thread.setName(threadName);
            return thread;
        });
        executor.initialize();
        return executor;
    }

    /**
     * 心跳检测任务池
     */
    @Bean(name = TaskBeanNameCommon.HAT_CHECK)
    public ThreadPoolTaskExecutor CheckHatTaskPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(1);
        executor.setMaxPoolSize(5);
        executor.setQueueCapacity(10);
        executor.setThreadFactory((runnable) -> {
            Thread thread = new Thread(runnable);
            String threadName = "hatCheck" + "-" + thread.getName();
            thread.setName(threadName);
            return thread;
        });
        executor.initialize();
        return executor;

    }


}       
